<?php

namespace app\common\controller;

use org\Auth;
use think\Loader;
use think\Cache;
use think\Controller;
use think\Db;
use think\Session;

/**
 * 后台公用基础控制器
 * Class AdminBase
 * @package app\common\controller
 */
class AgentBase extends Controller
{
    protected $bet_type = [
        'single' => '单',
        'double' => '双',
        'big' => '大',
        'small' => '小',
        'two_star' => '二星定位'
    ];

    protected $bet_unit = [
        'wan' => '万',
        'qian' => '千',
        'bai' => '百',
        'shi' => '十',
        'ge' => '个'
    ];

    protected $bet_status = [
        0 => '<span style="color:#009688">未开奖</span>',
        1 => '<span style="color:#5FB878">中奖</span>',
        2 => '<span style="color:#FF5722">未中奖</span>'
    ];

    protected function _initialize()
    {
        parent::_initialize();

        $this->checkAuth();
        $this->getMenu();

        // 输出当前请求控制器（配合后台侧边菜单选中状态）
        $this->assign('controller', Loader::parseName($this->request->controller()));
        $this->assign('bet_status', $this->bet_status);
        $this->assign('bet_type', $this->bet_type);
        $this->assign('bet_unit', $this->bet_unit);
    }

    /**
     * 权限检查
     * @return bool
     */
    protected function checkAuth()
    {
        $config = [
            'auth_on' => 1, // 权限开关
            'auth_type' => 1, // 认证方式，1为实时认证；2为登录认证。
            'auth_group' => 'auth_group_agent', // 用户组数据表名
            'auth_group_access' => 'auth_group_access_agent', // 用户-用户组关系表
            'auth_rule' => 'auth_rule_agent', // 权限规则表
            'auth_user' => 'user', // 用户信息表
        ];

        if (!Session::has('agent_id')) {
            $this->redirect('agent/login/index');
        }

        $module = strtolower($this->request->module());
        $controller = strtolower($this->request->controller());
        $action = strtolower($this->request->action());

        // 排除权限
        $not_check = ['agent/index/index', 'agent/authgroup/getjson', 'agent/system/clear'];

        if (!in_array($module . '/' . $controller . '/' . $action, $not_check)) {
            $auth = new Auth();
            $auth->setConfig($config);
            $admin_id = Session::get('agent_id');
            if (!$auth->check($module . '/' . $controller . '/' . $action, $admin_id) && $admin_id != 1) {
                $this->error('没有权限');
            }
        }
    }

    /**
     * 获取侧边栏菜单
     */
    protected function getMenu()
    {
//        $menu     = [];
//        $admin_id = Session::get('admin_id');
//        $auth     = new Auth();
//
//        $auth_rule_list = Db::name('auth_rule')->where('status', 1)->order(['sort' => 'DESC', 'id' => 'ASC'])->select();
//
//        foreach ($auth_rule_list as $value) {
//            if ($auth->check($value['name'], $admin_id) || $admin_id == 1) {
//                $menu[] = $value;
//            }
//        }
//        $menu = !empty($menu) ? array2tree($menu) : [];

        $menu = [
            [
                'id' => 1,
                'name' => 'agent/user/default',
                'title' => '用户管理',
                'type' => 1,
                'status' => 1,
                'pid' => 0,
                'icon' => 'fa fa-users',
                'sort' => 1,
                'condition' => '',
                'children' => [
                    [
                        'id' => 2,
                        'name' => 'agent/User/index',
                        'title' => '用户管理',
                        'type' => 1,
                        'status' => 1,
                        'pid' => 0,
                        'icon' => 'fa fa-users',
                        'sort' => 1,
                        'condition' => '',
                    ]
                ]
            ],
            [
                'id' => 1,
                'name' => 'agent/room/default',
                'title' => '游戏管理',
                'type' => 1,
                'status' => 1,
                'pid' => 0,
                'icon' => 'fa fa-gamepad',
                'sort' => 1,
                'condition' => '',
                'children' => [
                    [
                        'id' => 2,
                        'name' => 'agent/room/index',
                        'title' => '房间列表',
                        'type' => 1,
                        'status' => 1,
                        'pid' => 0,
                        'icon' => '',
                        'sort' => 1,
                        'condition' => '',
                    ]
                ]
            ],

        ];

        $this->assign('menu', $menu);
    }
}